package com.twu.biblioteca;

/**
 * Created with IntelliJ IDEA.
 * User: PREETHI
 * Date: 14/5/13
 * Time: 2:55 PM
 * To change this template use File | Settings | File Templates.
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class welcome extends JApplet  {
    JFrame frame;
    JMenuBar menuBar;
    JMenu File, books, member,movies;
    JMenuItem menuItem;
    JLabel label;
    JPanel contentPane;
    library lib=new library();
    framebooks fb=new framebooks(lib);
    framemovies fm=new framemovies(lib);
    membership mem=new membership(lib);


    public welcome()
    {

        frame = new JFrame("Biblioteca");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setJMenuBar(createMenuBar());
         frame.setContentPane(createContentPane());
        frame.setSize(1000,500);
        frame.setVisible(true);

    }
    public Component getMenus()
    {
        return frame.getJMenuBar();
    }
    public Component getMovies()
    {
        return fm.movieTable;
    }
    public Component getBooks()
    {
        return fb.bookTable;
    }
    public Container createContentPane() {
        contentPane = new JPanel(new BorderLayout());
        contentPane.setOpaque(true);
        label = new JLabel("WELCOME TO BIBLIOTECA",JLabel.CENTER);
        label.setName("welcomemsg");
        label.setFont(new Font("Serif", Font.PLAIN, 28));
        label.setVisible(true);
        contentPane.add(label, BorderLayout.CENTER);
        return contentPane;
    }
    public JMenuBar createMenuBar()
    {
        menuBar=  new JMenuBar();
        menuBar.setName("Menubar");
        File=new JMenu("File");
        File.setName("file");
        menuBar.add(File);
        menuItem = new JMenuItem("Exit");
        menuItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        File.add(menuItem);

        books =new JMenu("Books");
        books.setName("books");
        menuBar.add(books);
        menuItem=new JMenuItem("View Books");
        menuItem.setName("view books");
        books.add(menuItem);
        menuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.getContentPane().removeAll();
                frame.setContentPane(fb.listBooks());
                frame.repaint();
                frame.setVisible(true);
            }
        });

        menuItem=new JMenuItem("Add books");
        menuItem.setName("add books");
        menuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(contentPane,"Invalid Option! You should login as Librarian","Invalid",JOptionPane.INFORMATION_MESSAGE);
            }
        });
        books.add(menuItem);



        movies=new JMenu("Movies");
        movies.setName("Movies");
        menuItem=new JMenuItem("View Movies");
        menuItem.setName("view movies");
        menuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.getContentPane().removeAll();
                frame.setContentPane(fm.listMovies());
                frame.repaint();
                frame.setVisible(true);
            }
        });
        movies.add(menuItem);
        menuBar.add(movies);

        member=new JMenu("Membership");
        member.setName("membership");
        menuBar.add(member);
        menuItem=new JMenuItem("View Membership");
        menuItem.setName("viewmembership");
        menuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.getContentPane().removeAll();
                frame.setContentPane(mem.viewMembership());
                frame.repaint();
                frame.setVisible(true);

            }
        });
        member.add(menuItem);
        menuItem=new JMenuItem("Login");
        menuItem.setName("login");
        menuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(lib.getUsername() ==null){
                frame.getContentPane().removeAll();
                frame.setContentPane(new framelogin(lib).displayForm());

                frame.repaint();
                frame.setVisible(true);
                }
                else
                    JOptionPane.showMessageDialog(frame,"Invalid Option!You are already logged in!","failure",JOptionPane.INFORMATION_MESSAGE);
            }
        });

        member.add(menuItem);

        menuItem=new JMenuItem("Logout");
        menuItem.setName("logout");
        menuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(lib.getUsername() !=null)
                {
                            lib.setUsername(null);
                            JOptionPane.showMessageDialog(frame,"You have successfully logged out","success",JOptionPane.INFORMATION_MESSAGE);
                }
                else
                    JOptionPane.showMessageDialog(frame,"You must be logged in first!","failure",JOptionPane.INFORMATION_MESSAGE);
            }
        });
        member.add(menuItem);
        return menuBar;
    }


            }




